package com.example.takeaway.util;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.Types;
import java.util.Collections;

/**
 * @author 邢晨旭
 * {@code @date} 2023/10/12
 */
public class AutoGenerator {
    public static void main(String[] args) {
        FastAutoGenerator.create("jdbc:mysql://127.0.0.1:3306/takeaway?characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai", "root", "123456")
                .globalConfig(builder -> {
                    builder.author("邢晨旭") // 设置作者
                            //.enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir("D://AutoGenerator"); // 指定输出目录
                })
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.TINYINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    }
                    return typeRegistry.getColumnType(metaInfo);

                }))
                .packageConfig(builder -> {
                    builder.parent("com.example.takeaway") // 设置父包名
                            //.moduleName("system") // 设置父包模块名
                            .entity("model.bo")
                            .service("service")
                            .serviceImpl("service.impl")
                            .mapper("mapper")
                            .controller("api")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, "D://AutoGenerator//mapper")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.entityBuilder()
                            .superClass(com.example.takeaway.model.bo.BaseBO.class)
                            .addSuperEntityColumns("is_deleted", "create_time", "update_time", "creator", "updater")
                            .disableSerialVersionUID()
                            .enableFileOverride()
                            .enableLombok()
                            .enableTableFieldAnnotation()
                            .idType(IdType.AUTO)
                            .build()
                            .controllerBuilder()
                            .enableFileOverride()
                            .enableRestStyle()
                            .formatFileName("%sApi")
                            .build()
                            .serviceBuilder()
                            .enableFileOverride()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            .build()
                            .mapperBuilder()
                            .enableFileOverride()
                            .enableBaseResultMap()
                            .formatMapperFileName("%sMapper")
                            .formatXmlFileName("%sMapper")
                            .build()
                    ;
                })
                // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}
